package com.android.sharecar.activity;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class RouteMapActivity extends MapActivity {
	public static final String TAG = "RouteMapActivity";

	// Views
	private MapView mRouteMap;
	private Button mClose;

	// About map
	private static GeoPoint mBeginPoint, mEndPoint;
	private static Bitmap mBeginBitmap, mEndBitmap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_route);

		findViews();

		setListenerForViews();

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (null != bundle && bundle.containsKey("myLocation")) {
			Double geoLat;
			Double geoLon;
			if (bundle.containsKey("geoLat") && bundle.containsKey("geoLon")) {
				geoLat = bundle.getDouble("geoLat") * 1E6;
				geoLon = bundle.getDouble("geoLon") * 1E6;
				mBeginPoint = new GeoPoint(geoLat.intValue(), geoLon.intValue());
				mEndPoint = new GeoPoint(geoLat.intValue() + 5000,
						geoLon.intValue() + 50000);
			}
		}

		initialRouteMap();
	}

	private void setListenerForViews() {
		mClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				RouteMapActivity.this.finish();
			}
		});
	}

	private void findViews() {
		mClose = (Button) findViewById(R.id.routeMapActivity_btn_close);
		mRouteMap = (MapView) findViewById(R.id.routeMapActivity_map_routeMap);
	}

	public void initialRouteMap() {
		try {
			mBeginBitmap = ((BitmapDrawable) getResources().getDrawable(
					R.drawable.line_begin)).getBitmap();
			mEndBitmap = ((BitmapDrawable) getResources().getDrawable(
					R.drawable.line_end)).getBitmap();
		} catch (Exception e) {
			Log.v(TAG,
					"Load map bitmaps(beginPoint and endPoint) occurs Exception, message is :"
							+ e.getMessage());
		}

		// 指定起始和终止位置的经纬度
		Double geoLat = 40.0812286 * 1E6;
		Double geoLon = 116.3058265 * 1E6;

		mBeginPoint = new GeoPoint(geoLat.intValue(), geoLon.intValue());
		mEndPoint = new GeoPoint(geoLat.intValue() + 5000,
				geoLon.intValue() + 50000);
		if (null != mBeginPoint && null != mEndPoint) {
			mRouteMap.setBuiltInZoomControls(true);
			mRouteMap.setTraffic(true);
			mRouteMap.setStreetView(true);

			mRouteMap.getOverlays().add(
					new PointOverlay(mBeginPoint, mBeginBitmap));
			mRouteMap.getOverlays()
					.add(new PointOverlay(mEndPoint, mEndBitmap));
			mRouteMap.getOverlays()
					.add(new LineOverlay(mBeginPoint, mEndPoint));
			mRouteMap.getController().setCenter(mBeginPoint);
			mRouteMap.getController().setZoom(16);
		}
	}

	/**
			* 用于在地图上绘制线条
			* @author xinyan
			* @date 2011-10-15
			*/

	private class LineOverlay extends Overlay {
		private final GeoPoint begin;
		private final GeoPoint end;

		public LineOverlay(GeoPoint begin, GeoPoint end) {
			this.begin = begin;
			this.end = end;
		}

		@Override
		public void draw(Canvas canvas, MapView map, boolean shadow) {
			Paint paint = new Paint();
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			paint.setStrokeWidth(2);

			Point beginPoint = new Point();
			Point endPoint = new Point();
			Path path = new Path();

			mRouteMap.getProjection().toPixels(begin, beginPoint);
			mRouteMap.getProjection().toPixels(end, endPoint);
			path.moveTo(endPoint.x, endPoint.y);
			path.lineTo(beginPoint.x, beginPoint.y);

			canvas.drawPath(path, paint);
		}
	}

	/**
		* Used for painting a point in the map.
		* @author xinyan
		* @date 2011-10-15
		*/

	private class PointOverlay extends Overlay {
		private final GeoPoint geoPoint;
		private final Bitmap bitmap;

		public PointOverlay(GeoPoint geoPoint, Bitmap bitmap) {
			this.geoPoint = geoPoint;
			this.bitmap = bitmap;
		}

		@Override
		public void draw(Canvas canvas, MapView map, boolean shadow) {
			Point point = new Point();
			mRouteMap.getProjection().toPixels(geoPoint, point);
			Paint paint = new Paint();
			canvas.drawBitmap(bitmap, point.x, point.y, paint);
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
